<template>
  <Container class="rebate-checks-list">
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <div slot="extra">
        <Button
          size="large"
          class="item"
          type="primary"
          @click="onCheckAll">一键审核</Button>
          <export-btn @onExport="onExport" class="item"></export-btn>
      </div>
      <TabPane label="季度积分返利审核" name="tab1">
        <homeTable
          :list="list"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
        />
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import IntegralQuarterListData from './list/IntegralQuarterListData'
import HeadFormItem from './list/HeadFormItem'
import homeTable from '@/components/homeTable'
import { clearEmpty, formatDate } from '@/util'
export default {
  name: 'welfare/integralQuarter/list',
  components: {
    HeadFormItem,
    homeTable
  },
  data() {
    return {
      tab: "tab1",
      list: IntegralQuarterListData
    };
  },
  mounted() {
    this.getData();
    this.$bus.$on('detailRow', ({row}) => {this.detailRow(row)})
    this.$bus.$on('checkRow', ({row}) => {this.checkRow(row)})
  },
  destroyed() {
    this.$bus.$off()
  },
  methods: {
    onSearchBtnClick(form) {
      this.list.currentPage = 1
      this.list.form = form
      this.getData()
    },
    checkRow(row) {
      this.$router.push({
        name: 'welfare/integralQuarter/edit',
        query: {
          id: row.id,
          from: 'check'
        }
      })
    },
    detailRow(row) {
      this.$router.push({
        name: 'welfare/integralQuarter/edit',
        query: {
          id: row.id,
          from: 'look'
        }
      })
    },
    getData() {
      this.list.status = "loading"
      param = clearEmpty(param)
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam(),
        current: this.list.currentPage,
        size: this.list.pageSize
      }
      this.$ajaxGet("/assets/store_int_quarter/page", param).then(data => {
          this.list.data = []
          this.list.totalItem = Number(data.total)
          this.list.data = (data.records || []).map(item => {
            return {
              id: item.id,
              storeName: item.storeName,
              distributorName: item.distributorName,
              storeTypeName: item.storeTypeName,
              storeNo: item.storeNo,
              quarterPurchase: item.quarterPurchase,
              quarterTaskQuantity: item.quarterTaskQuantity,
              quarterRepairIntegral: item.quarterRepairIntegral,
              quarterRepairRebate: item.quarterRepairRebate,
              statusMeaning: item.statusMeaning,
              status: item.status,
              quarterRewardIntegral: item.quarterRewardIntegral,
              quarterRewardRebate: item.quarterRewardRebate
            }
          })
          this.list.status = "pass"
        }).catch(() => {
          this.list.status = "error"
        })
    },
    onPageChange(number) {
      this.list.currentPage = number
      this.getData()
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1;
      this.list.pageSize = pageSize;
      this.getData();
    },
    onCheckAll() {
      if (this.list.data.length === 0) {
        this.$Notice.warning({
          title: `暂无数据，无法一键审核`
        })
        return
      }
      if (!this.list.form.time || !this.list.form.quarter) {
        this.$Notice.warning({
          title: `请先选择年份和季度`
        })
        return
      }
      let param = {
        year: formatDate(this.list.form.time).split('-')[0],
        quarter: this.list.form.quarter
      }
      this.$Modal.confirm({
        title: '确认一键审核吗',
        content:  '确认一键审核搜索的结果？一键审核后，当前筛选的季度任务已完成的将自动把未完成任务月份返利审核给门店。且以上操作不可逆转。',
        onOk: () => {
          this.$jsonPost('/assets/store_int_quarter/audit/all_query', param).then(data => {
            if (!data.quantity) {
              this.$Notice.info({
                title: '没有需要审核的数据'
              })
            } else {
              this.$Modal.remove()
              setTimeout(() => {
                this.onAuditAll(param, data.estimateTimeValue)
              }, 300)
            }
          })
        }
      })
    },
    onAuditAll(param, estimateTimeValue) {
      this.$Modal.confirm({
        title: `确认`,
        content: `一键审核需要${estimateTimeValue},确认要一键审核吗？`,
        onOk: () => {
          this.$jsonPost('/assets/store_int_quarter/audit/all', param).then(() => {
            this.$Notice.success({
              title: '一键审核成功'
            })
            this.getData()
          })
        }
      })
    },
    formatParam() {
      if (!this.list.form.time || !this.list.form.quarter) {
        this.$Notice.warning({
          title: `请先选择年份和季度`
        })
        return
      }
      if (!this.list.form.status) {
        this.$Notice.warning({
          title: `请先选择审核状态`
        })
        return
      }
      let params = {
        year: formatDate(this.list.form.time).split('-')[0],
        quarter: this.list.form.quarter,
        storeTypeId: this.list.form.storeTypeId ? this.list.form.storeTypeId : null,
        provinceId: this.list.form.provinceId ? this.list.form.provinceId : null,
        cityId: this.list.form.cityId ? this.list.form.cityId : null,
        areaId: this.list.form.areaId ? this.list.form.areaId : null,
        distributorId: this.list.form.distributorId ? this.list.form.distributorId : null,
        status: this.list.form.status
      }
      return clearEmpty(params)
    },
    onExport() {
      if (this.list.data.length === 0) {
        this.$Notice.warning({
          title: `暂无数据，无法导出`
        })
        return
      }
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam()
      }
      this.$jsonPost('/assets/store_int_quarter/export', param).then(data => {
        this.list.status = 'pass'
        this.$Modal.success({
          title: '导出成功',
          content: '请到首页-导出列表处下载',
          onOk: () => {
            this.$router.push('/home/export/list')
          }
        })
      }).catch(err => {
        this.list.status = 'pass'
        this.$Notice.error({
          title: '导出失败'
        })
      })
    }
  },
  watch: {
  }
}
</script>
